package com.moxiao.yfjh.module.blind.service.privilege;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.controller.admin.privilege.vo.MemberPrivilegeCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.privilege.vo.MemberPrivilegeExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.privilege.vo.MemberPrivilegePageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.privilege.vo.MemberPrivilegeUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.privilege.MemberPrivilegeConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.privilege.MemberPrivilegeDO;
import com.moxiao.yfjh.module.blind.dal.mysql.privilege.MemberPrivilegeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.MEMBER_PRIVILEGE_NOT_EXISTS;

/**
 * 相亲会员等级特权 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberPrivilegeServiceImpl implements MemberPrivilegeService {

    @Resource
    private MemberPrivilegeMapper memberPrivilegeMapper;

    @Override
    public void createMemberPrivilege(List<MemberPrivilegeCreateReqVO> createReqVO) {
        // 插入
        List<MemberPrivilegeDO> convert = MemberPrivilegeConvert.INSTANCE.convert(createReqVO);
        Collection<MemberPrivilegeDO> memberPrivilegeDOS = new ArrayList<>(convert);
        memberPrivilegeMapper.insertBatch(memberPrivilegeDOS);
    }

    @Override
    public void updateMemberPrivilege(List<MemberPrivilegeUpdateReqVO> updateReqVO) {
        updateReqVO.forEach(memberPrivilegeUpdateReqVO -> {
            // 校验存在
            validateMemberPrivilegeExists(memberPrivilegeUpdateReqVO.getId());
            // 更新
            MemberPrivilegeDO updateObj = MemberPrivilegeConvert.INSTANCE.convert(memberPrivilegeUpdateReqVO);
            memberPrivilegeMapper.updateById(updateObj);
        });

    }

    @Override
    public void deleteMemberPrivilege(Long id) {
        // 校验存在
        validateMemberPrivilegeExists(id);
        // 删除
        memberPrivilegeMapper.deleteById(id);
    }

    private void validateMemberPrivilegeExists(Long id) {
        if (memberPrivilegeMapper.selectById(id) == null) {
            throw exception(MEMBER_PRIVILEGE_NOT_EXISTS);
        }
    }

    @Override
    public MemberPrivilegeDO getMemberPrivilege(Long id) {
        return memberPrivilegeMapper.selectById(id);
    }

    @Override
    public List<MemberPrivilegeDO> getMemberPrivilegeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberPrivilegeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberPrivilegeDO> getMemberPrivilegePage(MemberPrivilegePageReqVO pageReqVO) {
        return memberPrivilegeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberPrivilegeDO> getMemberPrivilegeList(MemberPrivilegeExportReqVO exportReqVO) {
        return memberPrivilegeMapper.selectList(exportReqVO);
    }

    @Override
    public MemberPrivilegeDO getPrivilege(String name, Long levelId) {
        LambdaQueryWrapper<MemberPrivilegeDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(MemberPrivilegeDO::getPrivilegeName, name);
        lqw.eq(MemberPrivilegeDO::getLevelId, levelId);
        lqw.last("LIMIT " + 1);
        return memberPrivilegeMapper.selectOne(lqw);
    }

}
